//
//  SearchWorkViewController.m
//  InviteJob
//
//  Created by chirui on 2017/11/8.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "SearchWorkViewController.h"
#import "MainTableViewCell.h"
#import "WorkDetailsViewController.h"

@interface SearchWorkViewController ()<UITableViewDelegate,UITableViewDataSource>{

    NSMutableArray * _dataArray;
    UITableView * _tableView;
    UITextField * searchText;
    int _page;
    int _totailPage;
    UIView * noBodyView;
    
    NSString * keywordStr;

}

@end

@implementation SearchWorkViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _page = 1;
    searchText.text = @"";
    keywordStr = @"";
    _dataArray = @[].mutableCopy;
    
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightItemWithTitle:@"搜索"  target:self action:@selector(searchClick:)];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_publie_return" target:self action:@selector(backClick)];
    
    self.navigationItem.titleView= [SearchView creatSearchTextFieldForDelegate:self Placeholder:@"搜索关键词"];
    
    noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-150, 200, 200) andMessageStr:@"没有相关消息！" onView:nil];
    
    [self getMessageListData];
}

-(void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    searchText = textField;
}

-(void)searchClick:(UIButton *)btn {
    
    [searchText resignFirstResponder];
    
    if (searchText.text.length==0||searchText.text==nil) {
      SHuiMBhudTextTimeStr(@"请输入关键字搜索");
        return;
    }
    [self creatTableView];
    _page = 1;
    keywordStr = searchText.text;
    
    [self getMessageListData];
    
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[MainTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MainTableViewCell reuseIdentifier]];

    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        
        [self getMessageListData];
    }];
    _tableView.mj_header = header;
    
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getMessageListData];
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    _tableView.mj_footer.hidden = NO;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 130;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * dic = _dataArray[indexPath.row];
    MainTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[MainTableViewCell reuseIdentifier] forIndexPath:indexPath];
    cell.workName.text = dic[@"cate_name"];
    cell.details.text = [NSString stringWithFormat:@"%@%@  %@  %@",dic[@"city_name"],dic[@"county_name"],dic[@"work_experience"],dic[@"education"]];
    cell.priceLabel.text = dic[@"salary"];
    cell.timeLabel.text = dic[@"update_time"];
    cell.companyName.text = dic[@"company_name"];
    cell.peopleNumber.text = dic[@"staff_number"];
    cell.tagLabel.text = dic[@"com_type"];
    cell.companyName.numberOfLines = 1;
    [cell.companyName sizeToFit];
    
    cell.tagLabel.frame = CGRectMake(CGRectGetMaxX(cell.companyName.frame)+5, CGRectGetMinY(cell.companyName.frame)+2, 30, 15);
    
    [cell.companyImage sd_setImageWithURL:ImageUrl(dic[@"company_logo"]) placeholderImage:ImageName(@"")];
    
    return cell;

    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = _dataArray[indexPath.row];
    WorkDetailsViewController * work =[[WorkDetailsViewController alloc]init];
    work.hidesBottomBarWhenPushed = YES;
    work.workIdStr=dic[@"zp_id"];
    [self.navigationController pushViewController:work animated:YES];
    
}

-(void)getMessageListData{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"perpage":@"10",
                           @"cate_name":keywordStr
                           };
    
    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(main_dataList_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage = [responseObject[@"datas"][@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                    noBodyView.hidden =YES;
                    if (_dataArray.count<10) {
                        
                        _tableView.mj_footer.hidden = YES;
                    }else {
                        _tableView.mj_footer.hidden = NO;
                    }
                }else {
                    _tableView.mj_footer.hidden = YES;
                    noBodyView.hidden =NO;
                    
                    [self.view addSubview:noBodyView];
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];

                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}


@end
